// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  // event: 
  // "Content":"abc"
  // "CreateTime":1687966368
  // "FromUserName":"ohAbj5Wmml_0q40ZtldqaTZ5Sqfk"
  // "MsgId":24165868769222610
  // "MsgType":"text"
  // "ToUserName":"gh_c98a2fa2e39c"
  await cloud.callFunction({
    name: 'errorHandler',
    data: {
      msg: {
        id1: event.userInfo.openId,
        id2: wxContext.OPENID
      }
    }
  })
  sendData = {
    "touser": event.userInfo.openId,
    "msgtype": "link",
    "link": {
      "title": "点击开始app",
      "description": "这是app的简介",
      "url": "这个是下载地址",
      "thumb_url": "这是图文信息的封面url"
    }
  }
  await cloud.openapi.customerServiceMessage.send(sendData)
  // sendData.touser = "ohAbj5c_BMEKRwGCA4p3i7YyuFGk"
  // await cloud.openapi.customerServiceMessage.send(sendData)
  return 'success'
}